import { t } from "@/locales";
import {Avatar, FormRule, PrimaryTableCol, SelectProps, Space, Tag} from "tdesign-vue-next";
import {fileView} from "@/api/upload";

/**
 * 搜索表单项
 */
export const searchParams: SearchParamsProps[] = [
    {
        label: t('pages.sysUser.username'),
        key: 'username',
    },
];

/**
 * 表格列
 */
export const columns: PrimaryTableCol[] = [
    {
        title: t('pages.sysUser.username'),
        ellipsis: true,
        align: 'center',
        colKey: 'username',
    },
    {
        title: t('pages.sysUser.sex'),
        ellipsis: true,
        align: 'center',
        colKey: 'sex',
        cell: (h, { row }) => h(Tag, {}, row.sex == 0 ? '男' : row.sex == 1 ? '女' : '未知')
    },
    {
        title: t('pages.sysUser.nickname'),
        ellipsis: true,
        align: 'center',
        colKey: 'nickname',
    },
    {
        title: t('pages.sysUser.phone'),
        ellipsis: true,
        align: 'center',
        colKey: 'phone',
    },
    {
        title: t('pages.sysUser.email'),
        ellipsis: true,
        align: 'center',
        colKey: 'email',
    },
    {
        title: t('pages.sysUser.role'),
        align: 'center',
        cell: (h, {row}) => h(Space, { direction: 'vertical' }, row.roles.map(i => h(Tag, { key: i.value }, i.label)))
    },
    {
        title: t('pages.sysUser.avatar'),
        ellipsis: true,
        align: 'center',
        colKey: 'avatar',
        cell: (h, {row}) => h(Avatar, { image: fileView(row.avatar) })
    },
    {
        title: t('pages.sysUser.remarks'),
        ellipsis: true,
        align: 'center',
        colKey: 'remarks',
    },
];

/**
 * 表单校验规则
 */
export const rules: Record<string, FormRule[]> = {
    username: [{required: true, message: t('pages.sysUser.required.username'), type: 'error', trigger: 'blur'}],
    password: [{required: true, message: t('pages.sysUser.required.password'), type: 'error', trigger: 'blur'}],
    email: [{email: true, message: t('pages.sysUser.required.email'), type: 'error', trigger: 'blur'}],
}

export const sexOptions: SelectProps['options'] = [
    {
        label: t('components.sex.male'),
        value: 0
    },
    {
        label: t('components.sex.female'),
        value: 1
    },
    {
        label: t('components.sex.unknown'),
        value: 2
    },
];
